package com.taoym.voucher.xcx.service.impl;

import com.alibaba.fastjson.JSON;
import com.taoym.voucher.common.constant.CommonConstant;
import com.taoym.voucher.common.utils.HttpClientUtils;
import com.taoym.voucher.common.utils.ThreadLocalUtils;
import com.taoym.voucher.xcx.entity.dto.XcxGetSearchDealsDTO;
import com.taoym.voucher.xcx.entity.dto.XcxGetSearchDealsShopDTO;
import com.taoym.voucher.xcx.entity.vo.XcxGetSearchDealsShopVO;
import com.taoym.voucher.xcx.entity.vo.XcxGetSearchDealsVO;
import com.taoym.voucher.xcx.service.SysUserService;
import com.taoym.voucher.xcx.service.XcxMtTuanDanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @coder taoym
 * @datetime 2021/6/7 14:59
 * @desc 美团团单信息服务接口
 */
@Service
@Slf4j
public class XcxMtTuanDanServiceImpl implements XcxMtTuanDanService {

    @Value("${jutuike.tuandan.province_all}")
    private String provinceAll;
    @Value("${jutuike.tuandan.province_cities}")
    private String provinceCities;
    @Value("${jutuike.tuandan.city_categories}")
    private String cityCategories;
    @Value("${jutuike.tuandan.search_deals}")
    private String searchDeals;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Map<String, String> cityCategories(Integer cityId) {
        Object redisObj = redisTemplate.opsForHash().get("XCX:CITY:CATEGORIES", cityId);
        if (!ObjectUtils.isEmpty(redisObj)) {
            return JSON.parseObject(JSON.toJSONString(redisObj), Map.class);
        }
        String resp = HttpClientUtils.doGet(this.cityCategories + "?pub_id=" + sysUserService.getSysUser().getPubId() + "&cityId=" + cityId);
        Map map = JSON.parseObject(JSON.toJSONString(JSON.parseObject(JSON.toJSONString(JSON.parseObject(resp).get("data"))).get("categories")), Map.class);
        redisTemplate.opsForHash().put("XCX:CITY:CATEGORIES", cityId, map);
        return map;
    }

    @Override
    public XcxGetSearchDealsShopVO searchDealsShop(XcxGetSearchDealsShopDTO data) {
        String uid = ThreadLocalUtils.get().getUid();
        Map<String, Object> param = new HashMap<>();
        param.put("cat0Id", data.getCat0Id());
        param.put("sid", uid);
        param.put("lng", data.getLng());
        param.put("lat", data.getLat());
        param.put("pub_id", sysUserService.getSysUser().getPubId());
        param.put("relation_flag_name", CommonConstant.RELATION_FLAG_NAME);
        param.put("cityId", data.getCityId());
        param.put("del_repeat", 2);
        param.put("keyWords", data.getKeyWords());
        param.put("page", data.getPage());
        param.put("size", data.getSize());
        String resp = HttpClientUtils.doPost(searchDeals, JSON.toJSONString(param));
        String dataJson = JSON.parseObject(resp).get("data").toString();
        return JSON.parseObject(dataJson, XcxGetSearchDealsShopVO.class);
    }

    @Override
    public XcxGetSearchDealsVO searchDeals(XcxGetSearchDealsDTO data) {

        String uid = ThreadLocalUtils.get().getUid();

        Map<String, Object> param = new HashMap<>();
        param.put("cat0Id", data.getCat0Id());
        param.put("sid", uid);
        param.put("lng", data.getLng());
        param.put("lat", data.getLat());
        param.put("pub_id", sysUserService.getSysUser().getPubId());
        param.put("relation_flag_name", CommonConstant.RELATION_FLAG_NAME);
        param.put("cityId", data.getCityId());
        param.put("shopIds", data.getShopIds());

        String resp = HttpClientUtils.doPost(searchDeals, JSON.toJSONString(param));
        String dataJson = JSON.parseObject(resp).get("data").toString();
        return JSON.parseObject(dataJson, XcxGetSearchDealsVO.class);
    }
}
